/********** System Includes ***************************************************/

#include <iostream>
#include <fstream>

/********** Library Includes **************************************************/

#include <opencv/cv.h>
#include <opencv/highgui.h>

/********** Local Includes ****************************************************/

#include "blobdetector.h"
#include "common.h"

/********** Namespaces ********************************************************/

using namespace cv;
using namespace std;

/********** Program Entry *****************************************************/

int main(int, char **) {
	Mat image = imread("input/pleo_rotate25.jpg");
	BlobDetector bd;
	vector<BlobDetector::Blob> blobs = bd.findBlobs(image);
	blobs = bd.filterForLargest(blobs);
	
	vector<Point> pixels = bd.pixels(image, blobs[0]);
	
	Mat final(image.size(), CV_8UC3);
	
	for (int row = 0; row < final.size().height; row++) {
		for (int col = 0; col < final.size().width; col++) {
			for (int channel = 0; channel < 3; channel++) {
				final.at<Vec3b>(row, col)[channel] = 0;
			}
		}
	}
	
	ofstream file;
	file.open("output/final.csv");
	
	for (vector<Point>::iterator it = pixels.begin(); it != pixels.end(); ++it) {
		file << it->x << ", " << it->y << endl;
		
		for (int channel = 0; channel < 3; channel++) {
			final.at<Vec3b>(it->y, it->x)[channel] = 255;
		}
	}
	
	SHOW(final);
	imwrite("output/final.jpg", final);
	file.close();
	
	cout << "Press 'q' to quit" << endl;
	while (waitKey(300) != 'q');
	
	return 0;
}